<?php
require_once("vcl/vcl.inc.php");
require_once("lib/db_connection.php");
require_once("Writer.php");
//Includes
use_unit("comctrls.inc.php");
use_unit("components4phpfull/jtjavascript.inc.php");
use_unit("components4phpfull/jtsitetheme.inc.php");
use_unit("platinumgrid/jtplatinumgrid.inc.php");
use_unit("forms.inc.php");
use_unit("extctrls.inc.php");
use_unit("stdctrls.inc.php");

//Class definition
class Vehicle_Laborline_List extends Page
{
       public $hfForm = null;
       public $JSNewWindowOpenMixed = null;
       public $hfType = null;
       public $JTJavaScript2 = null;
       public $windowOpen = null;
       public $JTLaborlistHidden = null;
       public $JTJavaScript1 = null;
       public $hiddenWO = null;
       public $JTSiteTheme1 = null;
       public $JTPlatinumVehicleLaborline = null;
       public $hiddenId = null;
       function edtSearchJSClick($sender, $params)
       {

       ?>
       //Add your javascript code here
       document.getElementById('edtSearch').value = '';
       <?php

       }

       function btnSearchClick($sender, $params)
       {
       redirect('veh_laborline_list.php?veh='.$this->hiddenId->getValue().'&WO='.$this->hiddenWO->getValue());
       }


       function btnExportClick($sender, $params)
       {
       $this->JTLaborlistHidden->exportGridToXLSDownload('laborline_list.xls');


       }

       function JTLaborlistHiddenShow($sender, $params)
       {
       $query_veh = "select * from vehicle_tbl where vehicle_id='".$this->hiddenId->getValue()."'";
       $result_veh = mysql_query($query_veh)or die(mysql_error());
       $row_veh = mysql_fetch_array($result_veh);


       $WO = $this->hiddenWO->getValue();
       $query1 = "select * from veh_laborline_tbl where workorder='".$this->hiddenWO->getValue()."'";
       $result = mysql_query($query1)or die(mysql_error());

       $arr = array();

       $arr[] = array(
                        '1' => 'Vehicle Num:'.' '.$row_veh['VIN'],
                        '2'=>' ',
                        '3' => ' ',
                        '4' => ' ',
                        '5' => ' ',
                        '6' => ' ',
                        '7' => ' ',
                        '8' => ' ',
                        '9' => ' ',
                        '10' => ' '
                    );


       $arr[] = array(
                        '1' => 'Plate Num:'.' '.$row_veh['veh_num'],
                        '2'=> ' ',
                        '3' => ' ',
                        '4' => ' ',
                        '5' => ' ',
                        '6' => ' ',
                        '7' => ' ',
                        '8' => ' ',
                        '9' => ' ',
                        '10' => ' '
                    );

       $arr[] = array(
                        '1' => 'Year:'.' '.$row_veh['year'],
                        '2'=>' ',
                        '3' => ' ',
                        '4' => ' ',
                        '5' => ' ',
                        '6' => ' ',
                        '7' => ' ',
                        '8' => ' ',
                        '9' => ' ',
                        '10' => ' '
                    );

       $arr[] = array(
                        '1' => 'Make:'.' '.$row_veh['manufacturer_name'],
                        '2'=>' ',
                        '3' => ' ',
                        '4' => ' ',
                        '5' => ' ',
                        '6' => ' ',
                        '7' => ' ',
                        '8' => ' ',
                        '9' => ' ',
                        '10' => ' '
                    );

       $arr[] = array(
                        '1' => 'Model:'.' '.$row_veh['model_name'],
                        '2'=> ' ',
                        '3' => ' ',
                        '4' => ' ',
                        '5' => ' ',
                        '6' => ' ',
                        '7' => ' ',
                        '8' => ' ',
                        '9' => ' ',
                        '10' => ' '
                    );

       $arr[] = array(
                        '1' => 'Type/Style:'.' '.$row_veh['style_name'],
                        '2'=> ' ',
                        '3' => ' ',
                        '4' => ' ',
                        '5' => ' ',
                        '6' => ' ',
                        '7' => ' ',
                        '8' => ' ',
                        '9' => ' ',
                        '10' => ' '
                    );

       $arr[] = array(
                        '1' => ' ',
                        '2'=>' ',
                        '3' => ' ',
                        '4' => ' ',
                        '5' => ' ',
                        '6' => ' ',
                        '7' => ' ',
                        '8' => ' ',
                        '9' => ' ',
                        '10' => ' '
                    );


       $arr[] = array(
                        '1' => 'WORKORDER - LABORLINE REPORT',
                        '2'=>' ',
                        '3' => ' ',
                        '4' => ' ',
                        '5' => ' ',
                        '6' => ' ',
                        '7' => ' ',
                        '8' => ' ',
                        '9' => ' ',
                        '10' => ' '
                    );

      $arr[] = array(
      '1' => 'Description',
      '2'=> 'Units',
      '3' => 'Cost/Unit',
      '4' => 'Total'
      );

       while($row = mysql_fetch_array($result))
       {

       $labor_unit = "<a href=veh_laborline_edit.php?veh=".$this->hiddenId->getValue()."&WO=".$WO."&laborline=".$row['labor_id'].">".$row['description']."</a>";
       $tools2 = "<img onclick=js_deleteLaborline('".$this->hiddenId->getValue()."','".$WO."','".$row['labor_id']."'); src='image/delete_orange.jpg' title='delete'>";
       //"<a href=veh_laborline_delete.php?veh=".$this->hiddenId->getValue()."&WO=".$WO."&laborline=".$row['labor_id']."><img src='image/delete_orange.jpg' title='delete' no border></a>";

       $arr[] = array(
                        '1' => $row['description'],
                        '2' => $row['labor_unit'],
                        '3' => $row['cost'],
                        '4' => $row['total']
                        );
       }

      $total_amount = mysql_query("SELECT SUM(total) as sum_amount FROM veh_laborline_tbl where workorder='".$this->hiddenWO->getValue()."'");
      $row_amount = mysql_fetch_array($total_amount);


      $arr[] = array(
                        '1' => TOTAL,
                        '4' => $row_amount['sum_amount']
                        );

       $this->JTLaborlistHidden->setCellData($arr);


       }

       function imgBackJSMouseOut($sender, $params)
       {

       ?>
       //Add your javascript code here
       document.images['imgBack'].src="image/btn_back.png"
       <?php

       }

       function imgBackJSMouseOver($sender, $params)
       {

       ?>
       //Add your javascript code here
       document.images['imgBack'].src="image/btn_back2.png"
       <?php

       }

       function imgAddJSMouseOut($sender, $params)
       {

       ?>
       //Add your javascript code here
       document.images['imgAdd'].src="image/add_new.png"
       <?php

       }

       function imgAddJSMouseOver($sender, $params)
       {

       ?>
       //Add your javascript code here
       document.images['imgAdd'].src="image/add_new_blue.png"
       <?php

       }


       function JTPlatinumVehicleLaborlineShow($sender, $params)
       {
       $WO = $this->hiddenWO->getValue();
       /*$query1 = "select * from veh_laborline_tbl where workorder='".$this->hiddenWO->getValue()."' and
       (labor_date like '%".$this->edtSearch->Text."%'
       or description like '%".$this->edtSearch->Text."%'
       or labor_unit like '%".$this->edtSearch->Text."%')";
       $result = mysql_query($query1)or die(mysql_error());*/

       $query1 = "select * from veh_laborline_tbl where workorder='".$this->hiddenWO->getValue()."'";
       $result = mysql_query($query1)or die(mysql_error());

       $arr = array();
       while($row = mysql_fetch_array($result))
       {

       //$labor_unit = "<a href=veh_laborline_edit.php?veh=".$this->hiddenId->getValue()."&WO=".$WO."&laborline=".$row['labor_id'].">".$row['labor_date']."</a>";

       /*if($this->hfType->getValue()=='edit')
       {
       $labor_unit = "<span onclick=openEndorsement(".$row['labor_id']."); style='color:blue; cursor:pointer; text-decoration: underline;'>".$row['labor_date']."</span>";
       }
       if($this->hfType->getValue()=='create')
       {
       $labor_unit = "<span onclick=openEditLaborline(".$row['labor_id']."); style='color:blue; cursor:pointer; text-decoration: underline;'>".$row['labor_date']."</span>";
       }*/

       if($this->hfType->getValue()=='edit')
       {
       $lab_type = 'edit';
       }
       if($this->hfType->getValue()=='create')
       {
       $lab_type = 'create';
       }
       $labor_unit = "<span onclick=openEditLaborline2('".$row['labor_id']."','".$lab_type."'); style='color:blue; cursor:pointer; text-decoration: underline;'>".$row['labor_date']."</span>";


       //FOR ENDORSEMENT
       $query_endo = "select * from endorsement_labor_tbl where veh_id='".$this->hiddenId->getValue()."' and wo_id='".$this->hiddenWO->getValue()."' and labor_id='".$row['labor_id']."'";
       $result_endo = mysql_query($query_endo)or die(mysql_error());
       $row_endo = mysql_fetch_array($result_endo);
       $endo_count = mysql_num_rows($result_endo);

       if($_SESSION['access_vehicle']==2)
       {
       if($this->hfForm->getValue()=='summary')
       {
       if($endo_count!=0)
       {
       $tools2 = "<img onclick=js_deleteLaborline('".$this->hiddenId->getValue()."','".$WO."','".$row['labor_id']."','".$this->hfType->getValue()."','".$this->hfForm->getValue()."'); src='image/delete.png' title='delete' style='cursor:pointer'>
       <img onclick=window.open('labor_endorsement_list.php?veh=".$this->hiddenId->getValue()."&WO=".$WO."&labor=".$row['labor_id']."','popup','width=688,height=344,scrollbars=yes,resizable=no,toolbar=no,directories=no,location=no,menubar=no,status=no,left=300,top=200'); src='image/endo.png' title='View Endorsements' style='cursor:pointer'>";
       }

       if($endo_count==0)
       {
       $tools2 = "<img onclick=js_deleteLaborline('".$this->hiddenId->getValue()."','".$WO."','".$row['labor_id']."','".$this->hfType->getValue()."','".$this->hfForm->getValue()."'); src='image/delete.png' title='delete' style='cursor:pointer'>";
       }
       }

       if($this->hfForm->getValue()=='order')
       {
       $tools2 = "<img onclick=js_deleteLaborline('".$this->hiddenId->getValue()."','".$WO."','".$row['labor_id']."','".$this->hfType->getValue()."','".$this->hfForm->getValue()."'); src='image/delete.png' title='delete' style='cursor:pointer'>";
       }
       }


       if($_SESSION['access_vehicle']==1)
       {
       if($this->hfForm->getValue()=='summary')
       {
       if($endo_count!=0)
       {
       $tools2 = "<img onclick=window.open('labor_endorsement_list.php?veh=".$this->hiddenId->getValue()."&WO=".$WO."&labor=".$row['labor_id']."','popup','width=688,height=344,scrollbars=yes,resizable=no,toolbar=no,directories=no,location=no,menubar=no,status=no,left=300,top=200'); src='image/endo.png' title='View Endorsements' style='cursor:pointer'>";
       }

       if($endo_count==0)
       {
       $tools2 = "";
       }
       }

       if($this->hfForm->getValue()=='order')
       {
       $tools2 = "";
       }
       }

       //END FOR ENDORSEMENT


       //"<a href=veh_laborline_delete.php?veh=".$this->hiddenId->getValue()."&WO=".$WO."&laborline=".$row['labor_id']."><img src='image/delete_orange.jpg' title='delete' no border></a>";

       $cost = $row['cost'];
       $total = $row['total'];
       $c_cost = $this->commafy($cost);
       $c_tot = $this->commafy($total);

       if($_SESSION['isAdmin']==1)
       {
       $arr[] = array(
                        'Date' => $labor_unit,
                        'Description' => $row['description'],
                        'Units' => $row['labor_unit'],
                        'Cost/Unit' => $c_cost,
                        'Amount' => $c_tot,
                        'Tools' => $tools2
                        );
       }

       if($_SESSION['isAdmin']!=1)
       {
       $arr[] = array(
                        'Description' => $row['description'],
                        'Units' => $row['labor_unit'],
                        'Cost/Unit' => $c_cost,
                        'Amount' => $c_tot
                        );
       }

       }

      $total_amount = mysql_query("SELECT SUM(total) as sum_amount FROM veh_laborline_tbl where workorder='".$this->hiddenWO->getValue()."'");
      $row_amount = mysql_fetch_array($total_amount);

      $tot_amt = $row_amount['sum_amount'];
      $ctot_amt = $this->commafy($tot_amt);

      $arr[] = array(
                        'Date' => TOTAL,
                        'Amount' => $ctot_amt
                        );

       $this->JTPlatinumVehicleLaborline->setCellData($arr);

       }


       function commafy($_)
       {
        return strrev( (string)preg_replace( '/(\d{3})(?=\d)(?!\d*\.)/', '$1,' , strrev( $_ ) ) );
       }



       function Vehicle_Laborline_ListCreate($sender, $params)
       {
       if($_SESSION['isAdmin']!=1)
       {
        $this->imgAdd->setVisible(false);
        $this->btnExport->Left = 950;
       }

       $id = $_GET['veh'];
       $WO = $_GET['WO'];
       $type = $_GET['type'];
       $form = $_GET['form'];
       $this->hiddenId->setValue($id);
       $this->hiddenWO->setValue($WO);
       $this->hfType->setValue($type);
       $this->hfForm->setValue($form);

       $result_order = mysql_query("select * from veh_workorder_tbl where workorder_id='".$this->hiddenWO->getValue()."'");
       $row_order = mysql_fetch_array($result_order);
       //$this->lblOdernum->Caption=$row_order['order_num'];

       $result_vin = mysql_query("SELECT * FROM vehicle_tbl WHERE vehicle_id='".$this->hiddenId->getValue()."'");
       $row_selected = mysql_fetch_array($result_vin);
       $label = $row_selected['VIN'];

       echo '<script language=javascript>
       window.parent.setWODetails();
       </script>';
       /*$this->lblVeh->Caption = $label;
       $this->lblyr->Caption = $row_selected['year'];
       $this->lblmake->Caption = $row_selected['manufacturer_name'];
       $this->lblmodel->Caption = $row_selected['model_name'];
       $this->lblPlateNum->Caption = $row_selected['veh_num'];

       $this->imgAdd->setLink('veh_laborline_create.php?veh='.$this->hiddenId->getValue().'&WO='.$this->hiddenWO->getValue());
       $this->imgBack->setLink('veh_workorder_list.php?veh='.$this->hiddenId->getValue().'&WO'.$this->hiddenWO->getValue());*/
       }


}

global $application;

global $Vehicle_Laborline_List;

//Creates the form
$Vehicle_Laborline_List=new Vehicle_Laborline_List($application);

//Read from resource file
$Vehicle_Laborline_List->loadResource(__FILE__);

//Shows the form
$Vehicle_Laborline_List->show();

?>